﻿using CompanyManageSystem.Model;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_DatabaseCon : Form
    {
        //连接字符串
        private string connectionString = "Data Source={0};Initial Catalog={1};User ID={2};Password={3};Connect Timeout={4};Persist Security Info=True;";

        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_DatabaseCon()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = GetConnectionString();
            try
            {
                conn.Open();
                MessageBox.Show("连接成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 保存连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            bool isModified = false;
            //需要引用System.Configuration
            if (ConfigurationManager.ConnectionStrings["PSContext"] != null)
            {
                isModified = true;
                MessageBox.Show("已经配置了");
            }
            ConnectionStringSettings mySettings = new ConnectionStringSettings("PSContext", this.GetConnectionString(), "System.Data.SqlClient");
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (isModified)
            {
                config.ConnectionStrings.ConnectionStrings.Remove("PSContext");
            }

            config.ConnectionStrings.ConnectionStrings.Add(mySettings);
            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("ConnectionStrings");

            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["Setting"].Value = "true";
            cfa.Save();

            ConfigurationManager.RefreshSection("appSettings");

            MessageBox.Show("当前操纵将自动关闭程序,请自行重启!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Environment.Exit(0);

            //Application.Restart();
        }

        /// <summary>
        /// 给连接字符串补充信息
        /// </summary>
        /// <param name="database"></param>
        /// <returns></returns>
        private string GetConnectionString(string database = "Company")
        {
            return string.Format(this.connectionString, this.EditServer.Text.Trim(), database, this.EditUser.Text.Trim(), this.EditPass.Text.Trim(), this.EditTime.Text.Trim());
        }

    }
}
